#include <iostream>
#include "AddressBook.h"
using namespace std;

void addContact(AddressBook *adb)
{
	//判断电话本是否满了
	if (adb->num == MAXN)
	{
		cout << "address book is full" << endl;
		return;
	}

	else
	{
        // 姓名
		string name;
		cout << "enter name:" << endl;
		cin >> name;
		adb->contact[adb->num].Name = name;

		cout << "enter sex:" << endl;
		cout << "1 -- male" << endl;
		cout << "2 -- female" << endl;

        // 性别
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				adb->contact[adb->num].Sex = sex;
				break;
			}
			cout << "invalid input, please enter again" << endl;
		}

		// 年龄
		cout << "enter age:" << endl;
		int age = 0;
		cin >> age;
		adb->contact[adb->num].Age = age;

		//联系电话
		cout << "enter phone number:" << endl;
		string phone = "";
		cin >> phone;
		adb->contact[adb->num].PhoneNumber = phone;

		//家庭住址
		cout << "enter address:" << endl;
		string address;
		cin >> address;
		adb->contact[adb->num].Addr = address;

		//更新通讯录人数
		adb->num++;

		cout << "successfully added" << endl;
	}
}

void showContact(AddressBook * adb)
{
	if (adb->num == 0)
	{
		cout << "there is no contact" << endl;
	}
	else
	{
		for (int i = 0; i < adb->num; i++)
		{
			cout << "name:" << adb->contact[i].Name << endl;
			cout << "sex:" << (adb->contact[i].Sex == 1 ? "男" : "女") << endl;
			cout << "age:" << adb->contact[i].Age << endl;
			cout << "phone number:" << adb->contact[i].PhoneNumber << endl;
			cout << "address:" << adb->contact[i].Addr << endl;
		}
	}
}

int isExist(AddressBook * adb, string name)
{
	for (int i = 0; i < adb->num; i++)
	{
		if (adb->contact[i].Name == name)
		{
			return i;
		}
	}
	return -1;
}

void deleteContact(AddressBook * adb)
{
	cout << "enter the contact you want to delete:" << endl;
	string name;
	cin >> name;

	int ret = isExist(adb, name);
	if (ret != -1)
	{
		for (int i = ret; i < adb->num; i++)
		{
			adb->contact[i] = adb->contact[i + 1];
		}
        adb->num--;
		cout << "successfully delete" << endl;
	}
	else
	{
		cout << "there is no such contact" << endl;
	}
}

void findContact(AddressBook * adb)
{
	cout << "enter the contact you want to find" << endl;
	string name;
	cin >> name;

	int ret = isExist(adb, name);
	if (ret != -1)
	{
		cout << "name:" << adb->contact[ret].Name << endl;
		cout << "sex:" << adb->contact[ret].Sex << endl;
		cout << "age:" << adb->contact[ret].Age << endl;
		cout << "phoneNumber:" << adb->contact[ret].PhoneNumber << endl;
		cout << "address:" << adb->contact[ret].Addr << endl;
	}
	else
	{
		cout << "there is no such contact" << endl;
	}
}

void modifyContact(AddressBook * adb)
{
	cout << "enter the name of the contact you want to modify" << endl;
	string name;
	cin >> name;

	int ret = isExist(adb, name);
	if (ret != -1)
	{
		//姓名
		string name;
		cout << "enter name:" << endl;
		cin >> name;
		adb->contact[ret].Name = name;

		cout << "enter sex:" << endl;
		cout << "1 -- male" << endl;
		cout << "2 -- female" << endl;

		//性别
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				adb->contact[ret].Sex = sex;
				break;
			}
			cout << "invalid input";
		}

		//年龄
		cout << "enter age:" << endl;
		int age = 0;
		cin >> age;
		adb->contact[ret].Age = age;

		//联系电话
		cout << "enter phone number:" << endl;
		string phone = "";
		cin >> phone;
		adb->contact[ret].PhoneNumber = phone;

		//家庭住址
		cout << "enter address:" << endl;
		string address;
		cin >> address;
		adb->contact[ret].Addr = address;

		cout << "successfully modified" << endl;
	}
	else
	{
		cout << "there is no such contact" << endl;
	}
}

void emptyContact(AddressBook * adb)
{
	adb->num = 0;
	cout << "the address book has been empty" << endl;
}